package com.thb.pay.comm.utils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thb.pay.comm.dto.PayChannelWeightDTO;
import com.thb.pay.core.utils.WeightRandomUtils;
import com.xiaoleilu.hutool.collection.CollUtil;
import javafx.util.Pair;

import java.util.List;
import java.util.Map;

/**
 * @类名称 PayChannelRandomUtils
 * @类描述 <pre>支付通道 权重轮询</pre>
 * @作者 lucky
 * @创建时间 2019/10/24
 * @版本 v1.01
 */
public class PayChannelRandomUtils {

    /**
     * @param payPassageWeightList: 权重集合信息
     * @方法名称 getRandomByWeight
     * @功能描述 获取权重比例信息
     * @作者: lucky
     * @创建时间: 17:49 2019/10/24
     * @return: com.thb.pay.comm.dto.PayChannelWeightDTO
     **/
    public static PayChannelWeightDTO getRandomByWeight(List<PayChannelWeightDTO> payChannelWeightList) {
        if (CollUtil.isNotEmpty(payChannelWeightList)) {
            List<Pair<Long, Integer>> pairList = Lists.newArrayList();
            Map<Long, PayChannelWeightDTO> payChannelWeightMap = Maps.newHashMap();
            payChannelWeightList.forEach(payChannelWeightDTO -> {
                payChannelWeightMap.put(payChannelWeightDTO.getChannelId(), payChannelWeightDTO);
                Pair pair = new Pair(payChannelWeightDTO.getChannelId(), payChannelWeightDTO.getWeight());
                pairList.add(pair);
            });
            Long channelId = (Long) WeightRandomUtils.build(pairList).random();
            return payChannelWeightMap.get(channelId);
        }
        return null;
    }


}
